package com.zhushenwudi.hmutils.net;

import com.zhushenwudi.hmutils.view.WaitingDialogView;
import ohos.app.Context;
import ohos.data.preferences.Preferences;

/**
 * 网络加载时弹窗显隐观察者
 */
public class PendingLiveData implements Preferences.PreferencesObserver {
    private static final String NET_LOADING = "网络加载中";
    private Context context;

    public PendingLiveData(final Context context) {
        this.context = context;
    }

    @Override
    public void onChange(Preferences sp, String key) {
        if ("pending".equals(key)) {
            boolean isPending = sp.getBoolean("pending", false);
            if (isPending) {
                WaitingDialogView.show(context, NET_LOADING);
            } else {
                if (context != null) {
                    WaitingDialogView.hide();
                }
            }
        }
    }
}
